package com.study.thread.juc.executor;

import java.util.concurrent.*;
import java.util.concurrent.locks.LockSupport;

/**
 * @author Ryan
 * @Description TODO
 * @createTime 2020年12月15日
 */
public class ThreadPoolExecutorDemo {
    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4,
            0L, TimeUnit.MINUTES, new ArrayBlockingQueue<>(10),
            Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        threadPool.execute(()-> System.out.println(Thread.currentThread().getName()+"：hello Thread Pool"));
//
//        Future<Integer> future = threadPool.submit(() -> {
//            System.out.println(Thread.currentThread().getName()+"：hello callable return 1");
//            return 1;
//        });
//        System.out.println(future.get());
//        assert threadPool.getActiveCount()==0;
//        assert threadPool.getMaximumPoolSize()==3;
//        assert threadPool.getCorePoolSize()==2;
//
//        threadPool.execute(()-> System.out.println(Thread.currentThread().getName()+"：hello Thread Pool"));
//
//        assert threadPool.getActiveCount()==1;

        for (int i = 0; i < 15; i++) {
            threadPool.execute(() -> {
                        {
                            try {
                                TimeUnit.SECONDS.sleep(4);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println(Thread.currentThread() + "：hello Thread Pool");
                    }
            );
        }

        new Thread(() -> {
            for (; ; ) {
                System.out.println(threadPool.getQueue().size());
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(threadPool.getQueue().size());
            }
        }).start();

//        assert threadPool.getActiveCount() == 1;
//        assert threadPool.getQueue().isEmpty();

        System.out.println("end");
//        threadPool.shutdown();
    }
}
